package com.yyn.Learning.controller;


import com.yyn.Learning.client.UcenterClient;
import com.yyn.Learning.entity.EduComment;
import com.yyn.Learning.service.EduCommentService;
import com.yyn.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author yyn
 * @since 2023-01-10
 */
@CrossOrigin
@RestController
@RequestMapping("/Learning/edu-comment")
public class EduCommentController {
    @Autowired
    private EduCommentService eduCommentService;
    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation("save评论")
    @PostMapping("saveComment")
    public R saveComment(@RequestBody EduComment eduComment, HttpServletRequest request) {
        String guli_token = request.getHeader("token");
        if(guli_token != null){//已登录
//        得到用户信息
        R memberInfoObj = ucenterClient.getMemberInfoByToken(guli_token);
        HashMap<String,String> memberInfo = (HashMap) memberInfoObj.getData().get("memberInfo");
        eduCommentService.saveComment(memberInfo, eduComment);
            return memberInfoObj.message("发送成功");
        }
        return R.error().message("用户未登录");

    }
    @ApiOperation("get评论")
    @GetMapping ("getComment/{courseId}/{teacherId}")
    public R getComment(@PathVariable String courseId,@PathVariable String teacherId){
        List<EduComment> comment = eduCommentService.getComment(courseId, teacherId);
        return R.success().data("comments",comment);
    }

}

